﻿using HBase;
using hconnect;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace Controller {
    public class ScanNetSynchro : ScanSynchro {
        protected ScanNetConnect connect = null;

        public ScanNetSynchro() {
            connect = TaskPool.get<ScanNetConnect>(getCNo());

            errorCode = "NoRead";
        }

        public void ClearScan() {
            status = null;
        }
        public override bool excute() {
            if (!begin) {
                return true;
            }
            if (status != null) {
                return true;
            }
            addSpan();

            if (StatusCache.test) {
                status = (ScanStatus)textParse(status);
            } else {
                status = read();
                if (status == null) {
                    return true;
                }

            }

            beforeSync(status);

            if (oneTimeSleep > 1000) {
                oneTimeSleep = 1000;
            }
            extralSleep();
            return true;
        }
        protected override int maxTime() {
            return 20;
        }
        public void StopScan() {
            begin = false;
        }

        public int err = 0;
        public override ScanStatus read() {
            consoleOnly(0, "begin scan " + t);
            if (connect.scan()) {
                consoleOnly(0, "scan sended" + t);
                try {
                    Thread.Sleep(200);
                    byte[] bytes = connect.Receive();
                    String code = Encoding.Default.GetString(bytes);
                    consoleOnly(0, "扫描" + code);
                    string @str = code.Replace("\r\n", "");
                    if (!@str.Contains(errorCode) && !string.IsNullOrWhiteSpace(@str)) {
                        err = 0;
                        return new ScanStatus(@str);
                    }
                } catch {
                    err += 1;
                }
            }
            return null;
        }
    }
    public class ScanNetConnect11 : ScanNetConnect {
    }
    public class ScanNetConnect21 : ScanNetConnect {
    }
    public class ScanNetConnect31 : ScanNetConnect {
    }
    public class ScanNetConnect41 : ScanNetConnect {
    }
    public class ScanNetSynchro11 : ScanNetSynchro {
        public ScanNetSynchro11() {

           // beginScan();
        }


    }
    public class ScanNetSynchro21 : ScanNetSynchro { }
    public class ScanNetSynchro31 : ScanNetSynchro { }
    public class ScanNetSynchro41 : ScanNetSynchro { }
}
